extends CenterContainer


var edit_data = null

onready var gym_list =  $PanelContainer/VBoxContainer/gyms/OptionButton
onready var drama_list =  $PanelContainer/VBoxContainer/dramas/OptionButton

signal close


func set_edit_data(data):
	self.edit_data = data
	gym_list.clear()
	drama_list.clear()
	
	var config = str2var(data["config"])

	for item in config["gym"]:
		var gym = DataSource.get_data_by_one(DataSource.gyms,item)
		gym_list.add_item(gym["name"],int(item))
		pass
	for item in config["drama_id"]:
		var drama = DataSource.get_data_by_one(DataSource.drama,str(item))
		drama_list.add_item(drama["name"],int(item))
		pass


func _on_ok_button_up():
	MainTool.getUILayer().pop_ui()
	emit_signal("close")


func _on_gym_select_item(index, value):
#	var gyms = DataSource.get_data_list(DataSource.gyms)
#	var select_gym = gyms[index]
#
#	var config = str2var(edit_data["config"])
#
#	for item in config["gym"]:
#		if int(item) == int(select_gym["id"]):
#			MainTool.toast_info("不可重复添加!")
#			return
#		pass
#	config["gym"].append(select_gym["id"])
#	gym_list.add_item(select_gym["name"],select_gym["id"])
#	# 恢复回去
#	edit_data["config"] = to_json(config)
#	DataSource.update_data(DataSource.conditions,edit_data)
	_process_select(DataSource.gyms,"gym",gym_list,index)
	pass

func _on_drama_select_item(index, value):
	_process_select(DataSource.drama,"drama_id",drama_list,index)
	pass

func _process_select(source_name,keys,ui_op_list,index):
	var temp_list = DataSource.get_data_list(source_name)
	var select = temp_list[index]
	
	var config = str2var(edit_data["config"])
	for item in config[keys]:
		if int(item) == int(select["id"]):
			MainTool.toast_info("不可重复添加!")
			return
		pass
	config[keys].append(int(select["id"]))
	ui_op_list.add_item(select["name"],int(select["id"]))
	# 恢复回去
	edit_data["config"] = to_json(config)
	DataSource.update_data(DataSource.conditions,edit_data)
	pass
func _on_Add_pressed(extra_arg_0):
	var flag = false
	var title = ""
	var list_data = []
	var func_name = ""
	if extra_arg_0 == "gym":
		func_name = "_on_gym_select_item"
		flag = true
		var gyms = DataSource.get_data_list(DataSource.gyms)
		title = "请选择徽章"
		for item in gyms:
			list_data.append(item["name"])
		pass
	elif extra_arg_0 == "drama":
		func_name = "_on_drama_select_item"
		flag = true
		
		var dramas = DataSource.get_data_list(DataSource.drama)
		title = "请选择剧本"
		for item in dramas:
			list_data.append(item["name"])
		pass

	if flag:
		var ui = MainTool.getUILayer().add_ui("res://tool/common/PopUp/ListSearchPopup.tscn", 0, false)
		ui.popup_centered()
		ui.set_data(title, list_data, funcref(self, func_name), -1)


func _on_Remove_pressed(extra_arg_0):
	var config = str2var(edit_data["config"])
	if extra_arg_0 == "gym":
		# 删除这个id
		var delete = gym_list.get_selected_id()
		config["gym"].erase(delete)
		
		pass
	elif extra_arg_0 == "drama":
		var delete = drama_list.get_selected_id()
		config["drama_id"].erase(delete)
		
		pass
	# 恢复回去
	edit_data["config"] = to_json(config)
	DataSource.update_data(DataSource.conditions,edit_data)
	set_edit_data(edit_data)
	pass # Replace with function body.
